package com.servlet.web;

import cn.hutool.crypto.SecureUtil;
import com.servlet.bean.Student;
import com.servlet.bean.User;
import com.servlet.service.StudentService;
import com.servlet.service.impl.StudentServiceImpl;
import com.servlet.utils.BaseServlet;
import com.servlet.utils.ProxyHelper;
import com.servlet.utils.ResponseVo;
import com.servlet.utils.ServletUtils;

import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Title: StudentController
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/26 14:30
 */
@WebServlet("/stu")
public class StudentController extends BaseServlet {

    private StudentService studentService = (StudentService) ProxyHelper.getProxyInstance(new StudentServiceImpl());
    /**
     * 检索全部的学生
     * @param req
     * @param resp
     * @throws IOException
     */
    public void queryStus(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            List<Student> students = studentService.queryStus();
            ServletUtils.writeResponse(resp, ResponseVo.ok("获取全部学生成功", students));
        } catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

    public void queryStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String id = req.getParameter("id");
            Student student = studentService.queryStuById(id);
            ServletUtils.writeResponse(resp, ResponseVo.ok("获取学生成功", student));
        } catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String isAdd = req.getParameter("isAdd");  // add
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String image = req.getParameter("image");
            Integer age = Integer.parseInt(req.getParameter("age"));
            Double score = Double.parseDouble(req.getParameter("score"));
            String comedate = req.getParameter("comedate");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse(comedate);
            String remark = req.getParameter("remark");
            Student student = new Student(id, name, image, age, score, d, remark);
            if ("add".equals(isAdd)) {
                studentService.addStu(student);
                ServletUtils.writeResponse(resp, ResponseVo.ok("新增学生成功", student));
            } else {
                studentService.editStu(student);
                ServletUtils.writeResponse(resp, ResponseVo.ok("修改学生成功", student));
            }
        } catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String id = req.getParameter("id");
            studentService.deleteStuById(id);
            ServletUtils.writeResponse(resp, ResponseVo.ok("删除学生成功"));
        } catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

}
